package dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import GsonBean.JcxxChakanBean;
import GsonBean.RjhSgxmListBean;
import GsonBean.SuccessBean;
import GsonBean.post.IsShutDownAuditPost;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zx on 2017/4/12.
 */
//是否同意停工审核
public class IsShutDownAuditDialog extends BaseDialog {

    //检查表详情数据
    JcxxChakanBean mJcxxChakanBean;

    List<RjhSgxmListBean> sgxmList;

    String tgxmmc;

    String tgxmid;

    RadioButton ty;
    RadioButton bty;
    EditText edit;

    TextView queding;
    TextView quxiao;
    TextView tgxm;

    TextView zgxx;
    LinearLayout zgxx_layout;

    //停工项目 spinner
    Spinner tgxmSpinner;
    boolean isSpinnerFirst=true;

    SpinnerAdapter mSpinnerAdapter;


    IsShutDownAuditPost mIsShutDownAuditPost;

    public IsShutDownAuditDialog(Context context, String tgxmmc, String tgxmid, JcxxChakanBean mJcxxChakanBean) {
        super(context);
        this.tgxmid = tgxmid;
        this.tgxmmc = tgxmmc;
        this.mJcxxChakanBean = mJcxxChakanBean;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_isshutdown);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        ty = (RadioButton) findViewById(R.id.tongyi);
        bty = (RadioButton) findViewById(R.id.butongyi);
        edit = (EditText) findViewById(R.id.edit);
        queding = (TextView) findViewById(R.id.queding);
        quxiao = (TextView) findViewById(R.id.quxiao);
        tgxm = (TextView) findViewById(R.id.tgxm);
        zgxx = (TextView) findViewById(R.id.zgxx);
        zgxx_layout = (LinearLayout) findViewById(R.id.zgxx_layout);
        tgxmSpinner = (Spinner) findViewById(R.id.tgxmspinner);
        sgxmList=new ArrayList<>();

        if (mJcxxChakanBean.getJcbShdwList() != null) {
            mSpinnerAdapter = new SpinnerAdapter();
            tgxmSpinner.setAdapter(mSpinnerAdapter);
//            根据是否选中 确定spinner中是否选中
//            根据是否为停工项目 向sgxmlist中添加初始数据
            for (int j = 0; j < mJcxxChakanBean.getRjhSgxmList().size(); j++) {
                RjhSgxmListBean bean = mJcxxChakanBean.getRjhSgxmList().get(j);
                if (bean.isTgXm()) {
                    sgxmList.add(bean);
                }
            }
            setTgxmText();
            tgxmSpinner.setVisibility(View.VISIBLE);
    } else {
        tgxmSpinner.setVisibility(View.GONE);
    }

}
        @Override
        protected void InitData () {
        mIsShutDownAuditPost = new IsShutDownAuditPost();

        //如果是停工审核不同意 将原先填写的数据 写入
        if (mJcxxChakanBean.getRjdjhjcbInfo().getJcbstatus().equals("0") || mJcxxChakanBean.getRjdjhjcbInfo().getJcbstatus().equals("14")) {
            if (mJcxxChakanBean.getRjdjhjcbInfo().getSfjytg().equals("1")) {
                ty.setChecked(true);
            } else {
                bty.setChecked(true);
            }
            edit.setText(mJcxxChakanBean.getRjdjhjcbInfo().getTgshyj());

            //对于停工不同意的计划，重新点击整改下达
            if (mJcxxChakanBean.getRjdjhjcbInfo().getZgqk() != null) {
                zgxx_layout.setVisibility(View.VISIBLE);
                zgxx.setText(mJcxxChakanBean.getRjdjhjcbInfo().getZgqk());
            }
        }

    }

        @Override
        protected void InitListen () {

                    //取消spinner默认选中项
        tgxmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (isSpinnerFirst) {
                    view.setVisibility(View.INVISIBLE);
                }
                isSpinnerFirst = false;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IsShutDownAuditDialog.this.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isAllowPost()) {
                    MyApplication.ShowWaitDialog(getContext());
                    mIsShutDownAuditPost.setJdjhjcbId(mJcxxChakanBean.getRjdjhjcbInfo().getJdjhjcbId());
                    if (ty.isChecked()) {
                        mIsShutDownAuditPost.setWhetherAuditAgree("Y");
                    } else {
                        mIsShutDownAuditPost.setWhetherAuditAgree("N");
                    }
                    mIsShutDownAuditPost.setTgxmmc(tgxm.getText().toString());
                    mIsShutDownAuditPost.setTgxmId(getTgxmid());
                    mIsShutDownAuditPost.setTgshyj(edit.getText().toString());
                    if (ty.isChecked()) {
                        mIsShutDownAuditPost.setZrDwMc(mJcxxChakanBean.getRjdjhjcbInfo().getZrDwMc());
                    }
                    mIsShutDownAuditPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());
                    mIsShutDownAuditPost.setFldw(mJcxxChakanBean.getRjdjhjcbInfo().getFldw());
                    mIsShutDownAuditPost.setFlrName(mJcxxChakanBean.getRjdjhjcbInfo().getFlrName());


                    //传入一个审核单位信息
                    IsShutDownAuditPost.LjyyxRjdjhjcbShdw jcbShdwBean = new IsShutDownAuditPost.LjyyxRjdjhjcbShdw();
                    jcbShdwBean.setJdjhbBh(mJcxxChakanBean.getRjdjhjcbInfo().getRjdjhbBh());
                    jcbShdwBean.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    jcbShdwBean.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    jcbShdwBean.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    jcbShdwBean.setShrname(MyApplication.getmLoginUser().getTrueName());
                    jcbShdwBean.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    List<IsShutDownAuditPost.LjyyxRjdjhjcbShdw> jcbShdwList = new ArrayList<IsShutDownAuditPost.LjyyxRjdjhjcbShdw>();
                    jcbShdwList.add(jcbShdwBean);
                    mIsShutDownAuditPost.setJcbShdwList(jcbShdwList);

                    NetWorkApi.TgShqr(mIsShutDownAuditPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    try {
                                        MyApplication.HideWaitDialog();
                                        SuccessBean mBean = response.body();
                                        if (mBean.getMsg() == null) {
                                            MyToast.showToast("服务器连接错误");
                                            return;
                                        }
                                        if (mBean.getMsg().equals("success") && mBean.getCount() != 0) {
                                            MyToast.showToast("停工审核成功");
                                            RefreshManager.Jdjc.RefreshJdjclrxx();
                                            RefreshManager.Jdjc.RefreshJdjcdcl();
                                            RefreshManager.Jdjc.RefreshJdjcycl();
                                            IsShutDownAuditDialog.this.dismiss();
                                            getContext().startActivity(new Intent(getContext(), JclrActivity.class));
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );


                }

            }
        });
    }


    public boolean isAllowPost() {

        if (!ty.isChecked() && !bty.isChecked()) {
            MyToast.showToast("请选择改项目是否停工");
            return false;
        }
        if (bty.isChecked() && edit.getText().toString().equals("")) {
            MyToast.showToast("不同意停工审核必须填写理由");
            return false;
        }
        if(sgxmList.size()==0){
            MyToast.showToast("请选择停工项目");
            return false;
        }

        return true;
    }
    class SpinnerAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mJcxxChakanBean.getRjhSgxmList().size();
        }

        @Override
        public Object getItem(int position) {
            return mJcxxChakanBean.getRjhSgxmList().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {


            ViewHolder mViewHolder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.bcfxd_listview, null);
                mViewHolder = new ViewHolder();
                mViewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }


            mViewHolder.mCheckBox.setText(mJcxxChakanBean.getRjhSgxmList().get(position).getSgxmmc());
            mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        addSgxm(position);
                    } else {
                        DelectSgxm(position);
                    }
                }
            });


            if (mJcxxChakanBean.getRjhSgxmList().get(position).isTgXm()) {
                mViewHolder.mCheckBox.setChecked(true);
            } else {
                mViewHolder.mCheckBox.setChecked(false);
            }
            return convertView;
        }

        class ViewHolder {
            CheckBox mCheckBox;

        }
    }
    public void addSgxm(int position) {
        if(!sgxmList.contains(mJcxxChakanBean.getRjhSgxmList().get(position)))
            sgxmList.add(mJcxxChakanBean.getRjhSgxmList().get(position));
        mJcxxChakanBean.getRjhSgxmList().get(position).setTgXm(true);

        String s = sgxmList.toString();
        s = s.substring(1, s.length() - 1);
        tgxm.setText(s);
    }

    public void DelectSgxm(int position) {
        if(sgxmList.contains(mJcxxChakanBean.getRjhSgxmList().get(position)))
            sgxmList.remove(mJcxxChakanBean.getRjhSgxmList().get(position));
        mJcxxChakanBean.getRjhSgxmList().get(position).setTgXm(false);
        String s = sgxmList.toString();
        s = s.substring(1, s.length() - 1);
        tgxm.setText(s);
    }

    public void setTgxmText(){
        String s="";
        for(RjhSgxmListBean item:mJcxxChakanBean.getRjhSgxmList()){
            if(item.isTgXm()){
                s+=item.getSgxmmc()+",";
            }
        }
        if(s.length()>0)
            s=s.substring(0,s.length()-1);
        tgxm.setText(s);
    }

    public String getTgxmid(){
        String s="";
        for(int i=0;i<mJcxxChakanBean.getRjhSgxmList().size();i++){
            if(mJcxxChakanBean.getRjhSgxmList().get(i).isTgXm()){
                s+=mJcxxChakanBean.getRjhSgxmList().get(i).getSgxmlbzdId()+",";
            }
        }
        if(s.length()>0){
            s=s.substring(0,s.length()-1);
        }

        return  s;
    }
}
